package com.pinganfu.mock.test;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;

/**
 * Created by Administrator on 2014/12/4.
 */
public  class GetThread extends Thread{
    private final HttpClient httpClient;
    private final HttpContext context;
    private final HttpHead httpHead;
    private final CountDownLatch threadSignal;
    private final ConcurrentHashMap<Integer,Boolean> hashMap;
    private static List<Thread> runningThreads = new ArrayList<Thread>();
    public GetThread(HttpClient httpClient, HttpHead httpHead, CountDownLatch threadSignal, ConcurrentHashMap<Integer, Boolean> hashMap) {
        this.httpClient = httpClient;
        this.context = new BasicHttpContext();
        this.httpHead = httpHead;
        this.threadSignal = threadSignal;
        this.hashMap = hashMap;
    }
    @Override
    public void run(){
        //System.out.println("abc:"+Thread.currentThread().getName());
        boolean b=get();
        hashMap.put((int) threadSignal.getCount(), b);
        threadSignal.countDown();
    }

    public boolean get() {
        HttpResponse response = null;
        try {
            response = httpClient.execute(httpHead);
            if(response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
                return true;
            }else{
                return false;
            }
        } catch (Exception e) {
            return false;
        }
    }

}
